package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import uteis.Constantes;
import model.InstCobranca;

public class InstCobrancaJDBCCadastro implements InstCobrancaDAO {

	Connection conexao;

	public InstCobrancaJDBCCadastro(Connection conexao) {
		this.conexao = conexao;
	}

	public String salvar(InstCobranca instcobranca) {
		String sqlquery = "insert into INSTCOBRANCA(NMINSTCOBRANCA, USUINCLUSAO, COMPINCLUSAO, USUALTERACAO, COMPALTERACAO, DTINCLUSAO, DTALTERACAO, CDEMPRESA)"
				+ " values(?,?,?,?,?,?,?,?);";

		PreparedStatement pstmt;
		try {

			pstmt = this.conexao.prepareStatement(sqlquery);
			pstmt.setString(1, instcobranca.getNminstcobranca());
			pstmt.setString(2, instcobranca.getUsuinclusao());
			pstmt.setString(3, instcobranca.getCompinclusao());
			pstmt.setString(4, instcobranca.getUsualteracao());
			pstmt.setString(5, instcobranca.getCompalteracao());
			pstmt.setDate(6, new java.sql.Date(instcobranca.getDtinclusao()
					.getTime()));
			// pstmt.setTime(8, new
			// java.sql.Time(formapagto.getHrinclusao().getTime()));
			pstmt.setDate(7, new java.sql.Date(instcobranca.getDtalteracao()
					.getTime()));
			// pstmt.setTime(10, new
			// java.sql.Time(formapagto.getHralteracao().getTime()));
			pstmt.setString(8, Constantes.cdempresas);
			pstmt.execute();

			return ("success");

		} catch (Exception e) {
			e.printStackTrace();
			return ("error");
		}
	}

	public ResultSet ResultInstCobranca() {

		// List<String> result = new ArrayList<String>();
		ResultSet valueinstcobra = null;
		// if (!nmformapagto.trim().equals("")) {
		String sql = "select CDINSTCOBRANCA, NMINSTCOBRANCA from INSTCOBRANCA where CDEMPRESA = "
				+ Constantes.cdempresas + ";";
		try {
			java.sql.Statement stmt = conexao.createStatement();
			valueinstcobra = stmt.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/**
		 * }else { System.out.println("VALOR EM BRANCO "+nmformapagto+
		 * " ------------- "+result ); }
		 **/
		return valueinstcobra;
	}

	public List<String> ListarInstCobranca(String nminstcobranca) {

		List<String> result = new ArrayList<String>();
		if (!nminstcobranca.trim().equals("")) {

			String sql = "SELECT CDINSTCOBRANCA, NMINSTCOBRANCA FROM INSTCOBRANCA WHERE NMINSTCOBRANCA like '%"
					+ nminstcobranca
					+ "%' AND CDEMPRESA = "
					+ Constantes.cdempresas + " ORDER BY NMINSTCOBRANCA ASC;";
			String snminstcobranca = "";
			String scdinstcobranca = "";
			try {
				java.sql.Statement stmt = conexao.createStatement();
				ResultSet valueinstcobranca = stmt.executeQuery(sql);

				valueinstcobranca.first();
				while (!valueinstcobranca.isAfterLast()) {
					snminstcobranca = valueinstcobranca
							.getString("NMINSTCOBRANCA");
					scdinstcobranca = valueinstcobranca
							.getString("CDINSTCOBRANCA");
					// result.add(scdinstcobranca+" | "+snminstcobranca);
					result.add(snminstcobranca);
					valueinstcobranca.next();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("VALOR EM BRANCO " + nminstcobranca
					+ " ------------- " + result);
		}
		return result;
	}

	public boolean localizarInstCobranca(String instcobranca) {
		String sqlquery = "SELECT NMINSTCOBRANCA FROM INSTCOBRANCA WHERE (NMINSTCOBRANCA like '"
				+ instcobranca
				+ "' and CDEMPRESA = "
				+ Constantes.cdempresas
				+ ") ORDER BY NMINSTCOBRANCA ASC;";
		try {
			String valor = "";
			java.sql.Statement stmt = conexao.createStatement();
			ResultSet rs = null;
			rs = stmt.executeQuery(sqlquery);
			while (rs.next()) {
				valor = rs.getString("NMINSTCOBRANCA");
			}
			if (valor.equals("")) {
				return (false);
			} else {
				return (true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return (false);
		}
	}

	public String LocalizaInstCobranca(String nminstcobranca) {

		String result = "";
		if (!nminstcobranca.trim().equals("")) {

			String sql = "SELECT CDINSTCOBRANCA FROM INSTCOBRANCA WHERE (NMINSTCOBRANCA like '"
					+ nminstcobranca
					+ "' and CDEMPRESA = "
					+ Constantes.cdempresas + ") ORDER BY CDINSTCOBRANCA ASC;";
			String scdinstcobranca = "";
			try {
				java.sql.Statement stmt = conexao.createStatement();
				ResultSet valueinstcobranca = stmt.executeQuery(sql);

				valueinstcobranca.first();
				scdinstcobranca = valueinstcobranca.getString("CDINSTCOBRANCA");
				// result.add(scdregistro);
				result = (scdinstcobranca);
				System.out.println("CODIGO INST COBRANCA " + scdinstcobranca
						+ " ------------- " + result);

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("VALOR EM BRANCO " + nminstcobranca
					+ " ------------- " + result);
		}
		return result;
	}

	public void editar(InstCobranca instcobranca) {
	}

	public void excluir(InstCobranca instcobranca) {
	}

	public List<InstCobranca> ListarInstCobrancaT() {
		return null;
	}

}